package cn.tedu.lucky.draw.server.job.config;

import org.omg.CORBA.PRIVATE_MEMBER;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.step.tasklet.TaskletStep;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class HelloJobConfig {

    /**
     * 作业启动器
     */
    @Autowired
    private JobLauncher jobLauncher;

    /**
     * Job构建工厂
     * 用于构建Job对象
     */
    @Autowired
    private JobBuilderFactory jobBuilderFactory;


    /**
     * Step构建工厂
     * 用于构建Step对象
     */
    @Autowired
    private StepBuilderFactory stepBuilderFactory;


    //构建一个Step对象
    @Bean
    public Step step() {
        return stepBuilderFactory.get("step")//为当前步骤取一个名字
                .tasklet((stepContribution, chunkContext) -> {
                    System.out.println("Hello，Spring Batch！");
                    return RepeatStatus.FINISHED;//表示步骤执行完成
                })
                .build();

    }


    //构建一个Job对象
    @Bean
    public Job job() {
        return jobBuilderFactory.get("hello-job")
                .start(step())
                .build();
    }

}
